/*
    S60Bible A Bible Reader for SymbianOS
    Copyright (C) 2004 Yohanes Nugroho

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

    Yohanes Nugroho (yohanes@opensource.or.id)
    Kp Areman RT 09/08 No 71 Desa Tugu Cimanggis Depok 16951 Indonesia
*/


#include "util.h"
#include "CFileFinderEngine.h"

CFileFinderEngine::CFileFinderEngine(RFs& aFs, RArray<BOOKITEM> &aFileList):iFileList(aFileList)
{
	iFs = aFs;
}

void CFileFinderEngine::ConstructL()
{
	iDirScan = CDirScan::NewL(iFs);
	iFs.DriveList(iDrivelist);
}

CFileFinderEngine::~CFileFinderEngine()
{
	delete iDirScan;
	delete iDirList;
}

TBool CFileFinderEngine::IsDone()
{
	return iDone;
}

void CFileFinderEngine::InitFind()
{

	_LIT(KDirFormat,"%c:\\");
	iCurrentDrive = 0;

	while (!iDrivelist[iCurrentDrive] && iCurrentDrive<26){
		iCurrentDrive++;
	}

	TBuf<3> buff;
	TChar c;
	iFs.DriveToChar(iCurrentDrive, c);
	buff.Format(KDirFormat, c);
	iDone = EFalse;
	//TRAPD(error, iDirScan->SetScanDataL(buff, KEntryAttNormal, ESortByName|EAscending, CDirScan::EScanDownTree));
	TRAPD(error, iDirScan->SetScanDataL(buff, KEntryAttMatchMask, ESortByName|EAscending, CDirScan::EScanDownTree));
	if (error!=KErrNone) {
		iDone = ETrue;
		return;
	}
	iIndex = 0;
	TRAPD(error2, iDirScan->NextL(iDirList));

	if (error2!=KErrNone) {
		iDone = ETrue;
		return;
	}
}

void CFileFinderEngine::CancelFind()
{
	iDone = ETrue;
}

TBool CFileFinderEngine::FindStep()
{
	_LIT(KPDB, "*.rdm");
	_LIT(Kpdb, "*.RDM");


	if (iDone) return ETrue;
	if (!iDirList) {
		iDone = ETrue;
		return ETrue;
	}

	if (iIndex>=iDirList->Count()){

		delete iDirList;
		iDirList = NULL;
		TRAPD(error2, iDirScan->NextL(iDirList));

		iIndex = 0;
		if (error2!=KErrNone) {
			delete iDirList;
			iDirList = NULL;
		}


        	while (iDirList==NULL) {

        		//Goto next available drive
        		do {
        			iCurrentDrive++;
        		} while (iCurrentDrive<26 && !iDrivelist[iCurrentDrive]);

        		if (iCurrentDrive>=26) {
        			delete iDirList;
        			iDone = ETrue;
        			return ETrue;
        		}
        		TDriveInfo driveInfo;
        		iFs.Drive(driveInfo, iCurrentDrive);
        		if (driveInfo.iDriveAtt==KDriveAttRom) continue;

        		_LIT(KDirFormat,"%c:\\");
        		TChar c;
        		iFs.DriveToChar(iCurrentDrive, c);
        		TBuf<3> buff;
        		buff.Format(KDirFormat, c);
        		//TRAPD(error, iDirScan->SetScanDataL(buff, KEntryAttNormal, ESortByName|EAscending, CDirScan::EScanDownTree));
				TRAPD(error, iDirScan->SetScanDataL(buff, KEntryAttMatchMask, ESortByName|EAscending, CDirScan::EScanDownTree));

        		if (error==KErrNone) {
        			TRAPD(error2, iDirScan->NextL(iDirList));
        			if (error2!=KErrNone) iDirList = NULL;
        		}
		}
	}

	if (iDirList && iIndex<iDirList->Count()) {
		TEntry entry = (*iDirList)[iIndex];
		/*char buff[300];
		DesToString(entry.iName, buff);
		{FILE *f = fopen("C:\\xxx.log", "a+");
		fprintf(f, "process file %s\n", buff);
		fclose(f);}
		*/
		if (!entry.IsSystem() && !entry.IsDir()) {
			if (entry.iName.Match(KPDB)!=KErrNotFound || entry.iName.Match(Kpdb)!=KErrNotFound){
			//found
				TBuf<256> fullname(iDirScan->FullPath());
				fullname.Append(entry.iName);
				///if (CheckBiblePDB::CheckFile(iFs, fullname)) {
				///	iStrList->AddStringL(fullname);
				///}
				///Message(fullname);
				BOOKITEM item;
				item.filepath.Copy(fullname);
				iFileList.Append(item);
			}
		}
	}
	iIndex++;
	return EFalse;
}

